﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Wheeler.Common.Extensions;
using Wheeler.Domain;
using Wheeler.Web.ViewModels;
using Wheeler.Web.Utility;
using AutoMapper;
using Wheeler.Web.ViewModels.UtilityModels;

namespace Wheeler.Web.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin")]
    public class UserController : BaseController
    {
		private WheelerEntities _Context = new WheelerEntities();
        
		#region Constructors
		public UserController()
		{
		}

		public UserController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index()
        {
            return View("List", BuildUserListConfig());
        }

		public ActionResult List()
        {
            return View(BuildUserListConfig());
        }

		/// <summary>
        /// Gets a User from Database to edit
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
		public ActionResult Edit(int id = 0, String IsSaved = null)
        {
            User entity = id == 0 ? new User() : _Context.Users.FirstOrDefault(P => P.ID == id);
            if (entity.ID == 0)
                entity.IsActive = true;
            UserViewModel viewModel = Mapper.Map<User, UserViewModel>(entity);             
            if (String.Compare(IsSaved, "true", true) == 0)
                ViewData[AppConstants.ViewDataKeys.SAVED] = true;
            if (viewModel != null)
                viewModel.Password = String.Empty;
			return View(viewModel);
        }

		/// <summary>
        /// Saves User Information into the Database
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
		[HttpPost]
		public ActionResult Edit(UserViewModel viewModel)
        {
			if (!ModelState.IsValid)
                return View(viewModel);

            if (_Context.IsDuplicateUserName(viewModel.UserName, viewModel.ID))
            {
                ModelState.AddModelError("UserName", "Sorry! the User Name already exists");
                return View(viewModel);
            }

			User entity = null; 
			if (viewModel.ID == 0)
            {
                entity = new User();
                _Context.AddToUsers(entity);
            }
            else
                entity = _Context.Users.FirstOrDefault(P => P.ID == viewModel.ID);

            entity = Mapper.Map<UserViewModel, User>(viewModel, entity);
            if (!viewModel.Password.IsEmpty())
                entity.Password = viewModel.Password;
		
            _Context.SaveChanges();
            //return RedirectToAction("Index");
			return RedirectToAction(String.Format("Edit/{0}", entity.ID), new { IsSaved = "true" });
        }		
		#endregion Action Methods
		
		#region Helper Methods

		/// <summary>
        /// Builds a ListConfig object with a list of User
        /// </summary>
        /// <returns></returns>
		private ListConfig BuildUserListConfig()
		{
			UtilityDAO dao = new UtilityDAO();
            //Wheeler.Domain.DbParameter[] parameters = new[]{
            //    //new Wheeler.Domain.DbParameter("@JobSeekerID", LoggedInUser.JobSeekerID)
            //};
            DataSet ds = dao.GetDataSet(DbObject.SP.GET_USER_LIST, null, true);
            ListConfig listConfig = new ListConfig(ds.Tables[0], null);
            return listConfig;
		}

        #endregion			
    }
}


